package cn.itcast.web.controller.company;

import cn.itcast.domain.company.Company;
import cn.itcast.service.company.CompanyService;
import cn.itcast.web.controller.BaseController;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/company")
public class CompanyController extends BaseController {


    @Autowired
    private CompanyService companyService;


    //查询全部
    @RequestMapping("/list")
    public String findAll (@RequestParam(defaultValue = "1")int page,@RequestParam(defaultValue = "5")int size){

        PageInfo info = companyService.findAll(page, size);
        request.setAttribute("page",info);
        return "company/company-list";


        //return "oss";
    }

    //进入添加
    @RequestMapping("/toAdd")
    public String toAdd(){

        return "company/company-add";
    }
    //添加
    @RequestMapping("/edit")
    public String save(Company company){
        if (StringUtils.isEmpty(company.getId())){
            companyService.save(company);
        }else {
            companyService.updateService(company);
        }

        return "redirect:/company/list.do";
    }

    //删除
    @RequestMapping("/delete")
    public String delete (String id){
        companyService.delete(id);

        return "redirect:/company/list.do";
    }

    //进入修改
    @RequestMapping("/toUpdate")
    public String toUpdate(String id){
        System.out.println(id);
        Company company = companyService.findBayId(id);
        request.setAttribute("company",company);
        return "company/company-update";
    }

    //分页
    @RequestMapping("")
    public String findPage(@RequestParam(defaultValue = "1")Integer page,@RequestParam(defaultValue = "5")Integer size){
        PageInfo pageInfo = companyService.findPage(page, size);
        return "system/dept/dept-list";
    }



}
